//
//  TPShare.m
//  Tiipvi
//
//  Created by xiaowuxiaowu on 2017/4/26.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "TPShare.h"
#import <OpenShare/OpenShare+QQ.h>
#import <OpenShare/OpenShare+Weixin.h>
#import <SDWebImage/SDWebImageDownloader.h>
#import <SDWebImage/SDWebImageManager.h>
#import "UIImage+Color.h"
#import "WXApi.h"
NSString *const QQ_key = @"1105646848";//@"1105346925";//1105529749
//NSString *const Weixin_key = @"wxd930ea5d5a258f4f";
//NSString *const WxAppSecret = @"d6553052fe7c9c3da2d32702f81f4656";
NSString *const Weixin_key = @"wx3b27f85b1974b7e2";
NSString *const WxAppSecret = @"95ec2a457ed1612eef2e7ddf2343ee1a";

@implementation TPShare

+(void)registWithShare{
    [OpenShare connectQQWithAppId:QQ_key];
    [OpenShare connectWeixinWithAppId:Weixin_key];
    
    //向微信注册wxd930ea5d5a258f4f
        [WXApi registerApp:Weixin_key];
    
}
+(BOOL)handleOpenURL:(NSURL *)url{
    
    return [OpenShare handleOpenURL:url];
    
}

+(BOOL)isWeixinInstalled{
  
    return [OpenShare isWeixinInstalled];
}

+(void)showTitle:(NSString *)title desc:(NSString *)desc image:(id )image link:(NSString *)link  type:(TPShareType)type Success:(shareSuccess)success Fail:(shareFail)fail{
    
    
    
    OSMessage *message = [[OSMessage alloc]init];
    message.title = title;
    message.link = xw_getNULLString(link);
    message.desc = xw_getNULLString(desc);
    
    NSString *tp_desc = @"来自旅影分享记录您人生最美影迹";
    
    if(message.desc.length<=0){
        
        message.desc = tp_desc;
    }else{
        message.desc = [message.desc stringByAppendingString:[NSString stringWithFormat:@"-%@",tp_desc]];
    }
    
    
    if(message.title.length<=0){
        message.title = tp_desc;
    }
    if(message.link.length<=0){
        message.link = @"http://www.gzchengxu.com";
    }
    
    DLog(@"分享链接--%@",message.link);
    NSData *data;
    if(!image){
        image = ImageNamed(@"icon");
    }
    
    if([image isKindOfClass:[UIImage class]]){
        
        
        data = UIImageJPEGRepresentation([image scaleImageToScale:0.3], IPHONE_WIDTH>375 ? 0.1 : 0.1);
        message.image = data;
        message.multimediaType = OSMultimediaTypeNews;
        [self actionPush:message type:type Success:success Fail:fail];
        
    }else if([image isKindOfClass:[NSString class]]){
        
        
        [[SDWebImageManager sharedManager].imageDownloader downloadImageWithURL:urlNamed(image) options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
            ;
        } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
            if(image){
                message.image = UIImageJPEGRepresentation([image scaleImageToScale:0.1], 0.1);//UIImageJPEGRepresentation([image scaleToSize:CGSizeMake(100, 100)], 0.5);
                message.multimediaType = OSMultimediaTypeNews;
                [self actionPush:message type:type Success:success Fail:fail];
            }else{
                [UIView xw_hideTitle:@"分享失败"];
            }
        }];
        
        //  data = [NSData dataWithContentsOfURL:urlNamed(image)];
        // DLog(@"---%lu",(unsigned long)data.length);
        
    }
    
    
    
    
    
}
+(void)actionPush:(OSMessage *)message type:(TPShareType)type Success:(shareSuccess)success Fail:(shareFail)fail{
    
    switch (type) {
        case TPShareTypeWXFriend:
        {
            [OpenShare shareToWeixinSession:message Success:success Fail:fail];
            
        }
            break;
        case TPShareTypeWXtimeline:
        {
            
            [OpenShare shareToWeixinTimeline:message Success:success Fail:fail];
            
            
        }
            break;
        case TPShareTypeQQFriend:
        {
            [OpenShare shareToQQFriends:message Success:success Fail:fail];
            
        }
            break;
        case TPShareTypeQQtimeline:
        {
            [OpenShare shareToQQZone:message Success:success Fail:fail];
            
        }
            break;
            
        default:
            break;
    }
    
    
}


@end
